//
//  ViewController.m
//  ChengDong
//
//  Created by Apple on 2019/10/18.
//  Copyright © 2019年 Apple. All rights reserved.
//

#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface ViewController ()<UIWebViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    webview.delegate = self;
//    webview.scrollView.scrollEnabled = NO;
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"companyCode.html" withExtension:nil];
    [webview loadRequest:[NSURLRequest requestWithURL:url]];
    
    for (id subview in webview.subviews){
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).bounces = NO;
        webview.scrollView.bounces = NO;
    }
    
    [self.view addSubview:webview];
    // Do any additional setup after loading the view, typically from a nib.
}

//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//    //调用OC原生方法
//    NSString *requestStr = [[request.URL absoluteString] stringByRemovingPercentEncoding];
//    NSLog(@"%@",requestStr);
//    if ([request.URL.scheme isEqualToString:@"home"]) {
//        //调用OC原生方法 如打开摄像头之类的方法
//        // .......
//        return NO;
//    }
//    return YES;
//}

//网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"网页加载完成");
    // 获取JS的上下文
    JSContext *context = [webView  valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    context[@"passtoken"] = ^() {
        // 获取到JSContext中的参数
        NSArray *args = [JSContext currentArguments];
        for (JSValue *jsVal in args) {
            NSString *result_Str = [jsVal toString];
            NSLog(@"字符串为%@", result_Str);
            JZSaveMyDefault(@"token", result_Str);
            [APPDELEGATE registerDeviceToken];
            // 拿到字符串做你想要做的操作 。注意当前是在子线程。所以必须回到主线程刷新UI
        }
        JSValue *content = [JSContext currentThis];
        NSLog(@"content为: %@",content);
    };
}
@end
